using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Barrage.Global
{
    /// <summary>
    /// Timer holds information about time passed between Clock() calls. It's widely used in the engine to measure time passed between to frames.
    /// A static instance is available via the DefaultTimer property, but other instances can be done to measure time in customized ways.
    /// </summary>
    public class Timer
    {
        static Timer defaultTimer = new Timer();
        /// <summary>
        /// Default timer instance
        /// </summary>
        public static Timer DefaultTimer { get { return defaultTimer; } }
        Stopwatch stopWatch=new Stopwatch();
        long time;
        long totalTime;
        /// <summary>
        /// Elapsed milliseconds between the two last Clock() calls
        /// </summary>
        public long LastInterval { get { return time; } }

        /// <summary>
        /// Elapsed seconds between the two last Clock() calls
        /// </summary>
        public float LastIntervalSeconds { get { return time / 1000.0f; } }

        /// <summary>
        /// Elapsed milliseconds between timer start and last Clock() call
        /// </summary>
        public long TotalTime { get { return totalTime; } }
        /// <summary>
        /// Elapsed seconds between timer start and last Clock call
        /// </summary>
        public float TotalTimeSeconds { get { return totalTime / 1000.0f; } }

        /// <summary>
        /// Elapsed milliseconds since timer start
        /// </summary>
        public long CurrentTotalTime { get { return stopWatch.ElapsedMilliseconds; } }
        /// <summary>
        /// Elapsed seconds since timer start
        /// </summary>
        public float CurrentTotalTimeSeconds { get { return stopWatch.ElapsedMilliseconds / 1000.0f; } }

        /// <summary>
        /// Constructor. Inits the timer count
        /// </summary>
        public Timer()
        {
            stopWatch.Start();
        }
        
        /// <summary>
        /// Stores the current time so the TotalTime and LastInterval properties change their values
        /// </summary>
        public void Clock()
        {
            time = stopWatch.ElapsedMilliseconds-totalTime;
            totalTime = stopWatch.ElapsedMilliseconds;
        }
    }
}
